<?php
namespace biz;
use database\DbHelper;
use utils\SysTool;
use model\FileInf;
use utils\PathTool;

class fd_scan
{
	var $root = null;//根节点
	var $sys = null;
	var $m_files = array();
	var $m_folders = array();
	var $m_coverFiles = array();
	var $m_coverFolders = array();
	
	function __construct() 
	{
	    $this->sys = new SysTool();
	}
	
	function GetAllFiles($parent,$root)
	{
	    $folder_gbk = $this->sys->to_gbk($parent->pathSvr);
	    $dir_handle=opendir($folder_gbk);
	    if($dir_handle)
		{
			while(($file=readdir($dir_handle))!==false)
			{
				if($file==='.' || $file==='..')
				{
					continue;
				}				
						
				$tmp = $this->sys->to_gbk($parent->pathSvr) . '/' . $file;
				if(is_dir($tmp))
				{
				    
					$fd = new FileInf();
					$pb = new PathBuilderUuid();
					$fd->id = $pb->guid();
					$fd->pid = $parent->id;
					$fd->uid = $parent->uid;
					$fd->pidRoot = $this->root->id;
					//$fd->nameSvr = iconv("gbk","utf-8",$file);
					$fd->nameSvr = $this->sys->to_utf8($file);
					$fd->nameLoc = $fd->nameSvr;
					$fd->pathSvr = $this->sys->to_utf8($this->sys->to_gbk($parent->pathSvr) . '/' . $file);
					$fd->pathSvr = str_replace("\\", "/", $fd->pathSvr);
					//$fd->pathRel = substr($fd->pathSvr, strlen($root) + 1);
					//$fd->pathRel = PathTool::combin($parent->pathRel, $fd->pathRel);
					$fd->pathRel = PathTool::combin($parent->pathRel, $fd->nameLoc);
					$fd->perSvr = "100%";
					$fd->complete = true;
					$this->m_folders[] = $fd;
									
					$this->GetAllFiles($fd, $root);
					
				} //文件
				else
				{
					$fl = new FileInf();
					$pb = new PathBuilderUuid();
					$fl->id = $pb->guid();
					$fl->pid = $parent->id;
					$fl->uid = $parent->uid;
					$fl->pidRoot = $this->root->id;
					//$fl->nameSvr = iconv("gbk","utf-8",$file);
					$fl->nameSvr = $this->sys->to_utf8($file);
					$fl->nameLoc = $fl->nameSvr;
					$fl->pathSvr = $this->sys->to_utf8($this->sys->to_gbk($parent->pathSvr) . '/' . $file);
					$fl->pathSvr = str_replace("\\","/", $fl->pathSvr);
					//$fl->pathRel = substr($fl->pathSvr, strlen($root) + 1);
					//$fl->pathRel = PathTool::combin($parent->pathRel, $fl->pathRel);
					$fl->pathRel = PathTool::combin($parent->pathRel, $fl->nameLoc);
					$fl->lenSvr = filesize( $this->sys->to_gbk($fl->pathSvr) );
					$fl->lenLoc = $fl->lenSvr;
					$fl->sizeLoc = PathTool::BytesToString($fl->lenSvr);
					$fl->perSvr = "100%";
					$fl->complete = true;
					$this->m_files[] = $fl;
				}
			}
			closedir($dir_handle);
		}		
	}
	
	function getCoverFiles($parent,$root)
	{
	    $folder_gbk = $this->sys->to_gbk($parent->pathSvr);
	    $dir_handle=opendir($folder_gbk);
	    if($dir_handle)
	    {
	        while(($file=readdir($dir_handle))!==false)
	        {
	            if($file==='.' || $file==='..')
	            {
	                continue;
	            }
	
	            $tmp = $this->sys->to_gbk($parent->pathSvr) . '/' . $file;
	            if(is_dir($tmp))
	            {
	
	                $fd = new FileInf();
	                //$pb = new PathBuilderUuid();
	                //$fd->id = $pb->guid();
	                //$fd->pid = $parent->id;
	                //$fd->pidRoot = $this->root->id;
	                //$fd->nameSvr = iconv("gbk","utf-8",$file);
	                $fd->nameSvr = $this->sys->to_utf8($file);
	                $fd->nameLoc = $fd->nameSvr;
	                $fd->pathSvr = $this->sys->to_utf8($this->sys->to_gbk($parent->pathSvr) . '/' . $file);
	                $fd->pathSvr = str_replace("\\", "/", $fd->pathSvr);
	                //$fd->pathRel = substr($fd->pathSvr, strlen($root) + 1);
	                //$fd->pathRel = PathTool::combin($parent->pathRel, $fd->pathRel);
	                $fd->pathRel = PathTool::combin($parent->pathRel, $fd->nameLoc);
	                //$fd->perSvr = "100%";
	                //$fd->complete = true;
	                $this->m_coverFolders[] = $fd->pathRel;
	                	
	                $this->getCoverFiles($fd, $root);
	                	
	            } //文件
	            else
	            {
	                $fl = new FileInf();
	                //$pb = new PathBuilderUuid();
	                //$fl->id = $pb->guid();
	                //$fl->pid = $parent->id;
	                //$fl->pidRoot = $this->root->id;
	                //$fl->nameSvr = iconv("gbk","utf-8",$file);
	                $fl->nameSvr = $this->sys->to_utf8($file);
	                $fl->nameLoc = $fl->nameSvr;
	                $fl->pathSvr = $this->sys->to_utf8($this->sys->to_gbk($parent->pathSvr) . '/' . $file);
	                $fl->pathSvr = str_replace("\\","/", $fl->pathSvr);
	                //$fl->pathRel = substr($fl->pathSvr, strlen($root) + 1);
	                //$fl->pathRel = PathTool::combin($parent->pathRel, $fl->pathRel);
	                $fl->pathRel = PathTool::combin($parent->pathRel, $fl->nameLoc);
	                //$fl->lenSvr = filesize( $this->sys->to_gbk($fl->pathSvr) );
	                //$fl->lenLoc = $fl->lenSvr;
	                //$fl->sizeLoc = PathTool::BytesToString($fl->lenSvr);
	                //$fl->perSvr = "100%";
	                //$fl->complete = true;
	                $this->m_coverFiles[] = $fl->pathRel;
	            }
	        }
	        closedir($dir_handle);
	    }
	}
	
	function save_files($db)
	{
		//bug:prepare中如果有返回值，则再次执行会报错。无论是否取完都无法再次执行。
		$sql = "
				insert into up6_files(
				 f_id
				,f_pid
				,f_pidRoot
				,f_fdTask
				,f_fdChild
				,f_uid
				,f_nameLoc
				,f_nameSvr
				,f_pathLoc
				,f_pathSvr
				,f_pathRel
				,f_md5
				,f_lenLoc
				,f_sizeLoc
				,f_lenSvr
				,f_perSvr
				,f_complete
				)
				values(
				 :id
				,:pid
				,:pidRoot
				,:fdTask
				,:fdChild
				,:uid
				,:nameLoc
				,:nameSvr
				,:pathLoc
				,:pathSvr
				,:pathRel
				,:md5
				,:lenLoc
				,:sizeLoc
				,:lenSvr
				,:perSvr
				,:complete
				)
				";
		$con =& $db->GetConUtf8();
		$cmd = $con->prepare($sql);
	
		foreach($this->m_files as $inf)
		{
		    $cmd->bindValue(":id", $inf->id );
		    $cmd->bindValue(":pid", $inf->pid );
		    $cmd->bindValue(":pidRoot", $inf->pidRoot );
		    $cmd->bindValue(":fdTask", false,\PDO::PARAM_BOOL);
		    $cmd->bindValue(":fdChild", true);//是文件夹中的文件
		    $cmd->bindValue(":uid", $inf->uid);
		    $cmd->bindValue(":nameLoc", $inf->nameLoc);
		    $cmd->bindValue(":nameSvr", $inf->nameSvr);
		    $cmd->bindValue(":pathLoc", $inf->pathLoc);
		    $cmd->bindValue(":pathSvr", $inf->pathSvr);
		    $cmd->bindValue(":pathRel", $inf->pathRel);
		    $cmd->bindValue(":md5", $inf->md5);
		    $cmd->bindValue(":lenLoc", $inf->lenLoc);
		    $cmd->bindValue(":sizeLoc", $inf->sizeLoc);
		    $cmd->bindValue(":lenSvr", $inf->lenSvr);
		    $cmd->bindValue(":perSvr", $inf->perSvr);
		    $cmd->bindValue(":complete", true,\PDO::PARAM_BOOL);
		    
		    if(!$cmd->execute())
		    {
		        print_r($cmd->errorInfo());
		    }
		}
	}
	
	function save_folders($db)
	{
		//bug:prepare中如果有返回值，则再次执行会报错。无论是否取完都无法再次执行。
		$sql = "
				insert into up6_folders(
				 f_id
				,f_pid
				,f_pidRoot
				,f_uid
				,f_nameLoc
				,f_pathLoc
				,f_pathSvr
				,f_pathRel
				,f_complete
				)
				values(
				 :id
				,:pid
				,:pidRoot
				,:uid
				,:nameLoc
				,:pathLoc
				,:pathSvr
				,:pathRel
				,:complete
				)
				";
		$con =& $db->GetConUtf8();
		$cmd = $con->prepare($sql);
		
		foreach($this->m_folders as $inf)
		{
    		$cmd->bindValue(":id", $inf->id );
    		$cmd->bindValue(":pid", $inf->pid );
    		$cmd->bindValue(":pidRoot", $inf->pidRoot);
    		$cmd->bindValue(":uid", $inf->uid);//是文件夹中的文件
    		$cmd->bindValue(":nameLoc", $inf->nameLoc);
    		$cmd->bindValue(":pathLoc", $inf->pathLoc);
    		$cmd->bindValue(":pathSvr", $inf->pathSvr);
    		$cmd->bindValue(":pathRel", $inf->pathRel );
    		$cmd->bindValue(":complete", true,\PDO::PARAM_BOOL);
    		
    		if(!$cmd->execute())
    		{
    			print_r($cmd->errorInfo());
    		}
		}
	}
	
	function cover_files()
	{
        //bug:prepare中如果有返回值，则再次执行会报错。无论是否取完都无法再次执行。
        $sql = "update up6_files set f_deleted=1 where f_pathRel=:f_pathRel";
        
        $db = new DbHelper();
        $con =& $db->GetConUtf8();
        $cmd = $con->prepare($sql);
        
        foreach($this->m_coverFiles as $f)
        {
            $cmd->bindValue(":f_pathRel", $f);
        
            if(!$cmd->execute())
            {
                print_r($cmd->errorInfo());
            }
        }
	}
	
	function cover_folders()
	{
	    //bug:prepare中如果有返回值，则再次执行会报错。无论是否取完都无法再次执行。
	    $sql = "update up6_folders set f_deleted=1 where f_pathRel=:f_pathRel";
	
	    $db = new DbHelper();
	    $con =& $db->GetConUtf8();
	    $cmd = $con->prepare($sql);
	
	    foreach($this->m_coverFolders as $f)
	    {
	        $cmd->bindValue(":f_pathRel", $f);
	
	        if(!$cmd->execute())
	        {
	            print_r($cmd->errorInfo());
	        }
	    }
	}
	
	function cover($inf, $pathParent)
	{
	    $this->getCoverFiles($inf, $pathParent);
        $this->cover_files();
        $this->cover_folders();
	}

	function scan($inf, $root)
	{
		$this->GetAllFiles($inf, $root);//扫描文件和目录
		
		$db = new DbHelper();
		$this->save_files($db);//保存文件列表
		$this->save_folders($db);//保存目录列表
	}	
}
?>